import { login } from '@/api/login'
import { checkMobile, register, checkAccount } from '@/api/register'

const user = {
  actions: {
    login ({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(username, userInfo.password).then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    Register ({ commit }, userInfo) {
      const phoneNumber = userInfo.phoneNumber.trim()
      return new Promise((resolve, reject) => {
        register(phoneNumber, userInfo.userName, userInfo.password).then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    CheckMobile ({ commit }, checkInfo) {
      const mobile = checkInfo.phoneNumber.trim()
      return new Promise((resolve, reject) => {
        checkMobile(mobile).then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    },
    CheckAccount ({ commit }, checkInfo) {
      const account = checkInfo.userName.trim()
      return new Promise((resolve, reject) => {
        checkAccount(account).then(response => {
          commit('')
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
